package jmind.dolphin.bootstrap;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.DefaultThreadFactory;
import jmind.base.util.GlobalConstants;
import jmind.dolphin.frame.ServiceFactory;
import jmind.dolphin.redis.server.DolphinRedisServer;
import jmind.dolphin.redis.server.handler.DolphinServerHandler;
import jmind.dolphin.redis.server.handler.RedisCommandDecoder;
import jmind.dolphin.redis.server.handler.RedisCommandHandler;
import jmind.dolphin.redis.server.handler.RedisReplyEncoder;
import jmind.dolphin.redis.server.SimpleRedisServer;
import jmind.dolphin.util.DophinUtil;

/**
 * Created by weibo.xwb on 2018/3/2.
 * https://www.cnblogs.com/smark/p/3247620.html
 */
public class DolphinServer {

    public static void main(String[] args) {
        try {
            run(9527);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static void run(int port) throws InterruptedException {


        // Only execute the command handler in a single thread
    //    final RedisCommandHandler commandHandler = new RedisCommandHandler(new SimpleRedisServer());
        final RedisCommandHandler commandHandler=new RedisCommandHandler(new DolphinRedisServer());
     //  final DolphinServerHandler commandHandler=new DolphinServerHandler();
        // Configure the server.
        ServerBootstrap b = new ServerBootstrap();
        final DefaultEventExecutorGroup group = new DefaultEventExecutorGroup(1);
        EventLoopGroup bossGroup = new NioEventLoopGroup(1, new DefaultThreadFactory("NettyServerBoss", true));
        EventLoopGroup workerGroup = new NioEventLoopGroup(GlobalConstants.NCPU+1,
                new DefaultThreadFactory("NettyServerWorker", true));
        try {
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 100)
                    .localAddress(port)
                    .childOption(ChannelOption.TCP_NODELAY, true)
                    .childHandler(new ChannelInitializer<NioSocketChannel>() {
                        @Override
                        public void initChannel(NioSocketChannel ch) throws Exception {
                            ChannelPipeline p = ch.pipeline();
//             p.addLast(new ByteLoggingHandler(LogLevel.INFO));
                            p.addLast(new RedisCommandDecoder());
                            p.addLast(new RedisReplyEncoder());
                            p.addLast(group, commandHandler);
                        }
                    });

            // Start the server.
            ChannelFuture f = b.bind(port).sync();

            // Wait until the server socket is closed.
            f.channel().closeFuture().sync();
        } finally {
            // Shut down all event loops to terminate all threads.
            group.shutdownGracefully();
        }
    }
}
